// 1. 导入express
const express = require("express");

// 2. 创建应用对象
const app = express();

app.use((req, res, next) => {
  let referer = req.get("referer");
  if (referer) {
    let url = new URL(referer);
    let hostname = url.hostname;
    if (hostname !== "127.0.0.1") {
      return res.status(404).send("<h1>404 Not Found</h1>");
    }
  }
  next();
});

// 静态中间件设置
app.use(express.static(__dirname + "/public"));

// 4. 监听端口，启动服务
app.listen(3000, () => {
  console.log("服务已经启动，端口 3000 正在监听中....");
});
